package com.zut.blocks;

import com.zut.blocks.base.common.Login;
import com.zut.blocks.base.enums.UserTypes;
import com.zut.blocks.base.util.UserUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @author lxd
 * @date 2020/12/6 14:29
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Configuration
    static class MyWebMvcConfigurer implements WebMvcConfigurer {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            // 拦截所有请求
            registry.addInterceptor(new HandlerInterceptorAdapter() {
                @Override
                public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                    Login login = (Login) request.getSession().getAttribute(UserUtil.KEY_USER);
                    if (login == null) {
                        request.getSession().invalidate();
                        response.sendRedirect("/");
                        return false;
                    }
                    return super.preHandle(request, response, handler);
                }
            }).addPathPatterns(Arrays.asList(
                    "/user/**",
                    "/api/**"
            ));
            // 拦截教师
            registry.addInterceptor(new HandlerInterceptorAdapter() {
                @Override
                public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                    Login login = (Login) request.getSession().getAttribute(UserUtil.KEY_USER);
                    if (login.getType().equals(UserTypes.tea)) {
                        response.sendRedirect("/user/" + login.getType());
                        return false;
                    }
                    return super.preHandle(request, response, handler);
                }
            }).addPathPatterns(Arrays.asList(
                    "/user/stu",
                    "/user/stu/**"
            ));

            // 拦截学生
            registry.addInterceptor(new HandlerInterceptorAdapter() {
                @Override
                public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                    Login login = (Login) request.getSession().getAttribute(UserUtil.KEY_USER);
                    if (login.getType().equals(UserTypes.stu)) {
                        response.sendRedirect("/user/" + login.getType());
                        return false;
                    }
                    return super.preHandle(request, response, handler);
                }
            }).addPathPatterns(Arrays.asList(
                    "/user/tea",
                    "/user/tea/**"
            ));
        }
    }
}
